<!-- 物流公司添加司机组件 -->
<template>
  <div class="app-container">
    <el-select v-model="driver" filterable remote reserve-keyword
      placeholder="输入司机手机号搜索"
      :remote-method="remoteMethod"
      :loading="loading"
      @change="change">
      <el-option style="height: 300%;"
        v-for="item in drivers"
        :key="item.id"
        :label="item.driverName"
        :value="item">
        <span>{{ item.driverName }}</span>
        <span style="float: right; color: #8492a6; font-size: 12px;">{{ idCardText(item.drivingLicense) }}</span>
      </el-option>
    </el-select>
  </div>
</template>

<script>
import { listDriverAll, addDriver } from "@/api/tws/driver";
import { hideIdCard } from '@/utils/index';

export default {
  name: "DriverAdd",
  data() {
    return {
      // 遮罩层
      loading: false,
      driver: '',
      drivers: []
    };
  },
  computed: {
    // 屏蔽身份证号
    idCardText(val) {
      return function(val) {
        if(val && val.length == 18) {
          return hideIdCard(val);
        } else {
          return val;
        }
      }
    }
  },
  methods: {
    // 搜索司机
    remoteMethod(query) {
      if (query !== '') {
        this.drivers = [];
        let obj = {};
        obj.telephone = query;
        obj.isCert = this.$store.state.constants.isCert_yrz;  // 已认证
        obj.state = '1';  // 状态正常
        this.loading = true;
        listDriverAll(obj).then(response => {
          this.drivers = response.rows;
          this.loading = false;
        });
      }
    },
    // 选中司机
    change(value) {
      this.$confirm('是否确认添加司机 "' + value.driverName + '" ?', "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return addDriver(value.id);
      }).then(() => {
        this.$modal.msgSuccess("添加成功");
        this.driverAdd();
      }).catch(err => {
        if(err.msg) {
          this.$modal.msgError(err.msg);
        }
      }).finally(() => {
        this.driver = '';
        this.drivers = [];
      })
    },
    // 回调
    driverAdd() {
      this.$emit("driverAdd");
    }
  }
};
</script>
